<template>
  <el-dialog
    :title="!dataForm.id ? $t('brand.add') : $t('user.editBtn')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="closeDialog"
    width="630px"
  >
    <el-form @submit.native.prevent
      class="dialog-form"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      :label-width="this.$i18n.t('language') === 'English' ? '150px' : '80px'"
    >
      <el-form-item :label="$t('outlet.serviceName')" prop="name">
        <el-input size="small" maxlength="36" v-model="dataForm.name" style="width:270px"></el-input>
      </el-form-item>

      <!-- <el-form-item :label="$t('outlet.classification')" prop="categoryId">
        <el-cascader
          expand-trigger="hover"
          :options="noticeContentList"
          :props="categoryTreeProps"
          :clearable="true"
          v-model="selectedCategory"
          @change="handleChange">
        </el-cascader>
      </el-form-item> -->
      <!-- <el-form-item :label="$t('outlet.status')" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">{{ $t("outlet.offline") }}</el-radio>
          <el-radio :label="1">{{ $t("outlet.normal") }}</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <div class="default-btn" @click="visible = false">{{
        $t("remindPop.cancel")
      }}</div>
      <div class="default-btn primary-btn" @click="dataFormSubmit()">{{
        $t("remindPop.confirm")
      }}</div>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data () {
    var validateTitle = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$i18n.t('shop.nameCanNoBlank')))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      id: 0,
      dataForm: {
        name: '',
        categoryId: '',
        status: 1
      },
      isSubmit: false,
      dataRule: {
        name: [
          { required: true, message: this.$i18n.t('shop.nameCanNoBlank'), trigger: 'blur' },
          {validator: validateTitle, trigger: 'blur'}
        ]
      },
      // 选项列表
      noticeContentList: [],
      // 选择的项目
      selectedCategory: [],
      categoryTreeProps: {
        value: 'categoryId',
        label: 'categoryName',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  components: {
  },
  mounted () {
  },
  methods: {
    init (id) {
      this.visible = true
      if (id) {
        this.id = id
        this.$http({
          url: this.$http.adornUrl('/shop/provision/info/' + this.id),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.dataForm = data
          this.selectedCategory = [data.categoryId]
        })
      }
      this.getNoticeContent()
    },
    // 获取分类选项
    getNoticeContent () {
      this.$http({
        url: this.$http.adornUrl('/prod/category/listCategory'),
        method: 'get',
        params: this.$http.adornParams({
          maxGrade: 0,
          status: 1
        })
      }).then(({data}) => {
        // 新增时对应分类要下拉选择“全部分类”或者某个一级分类
        this.noticeContentList = treeDataTranslate(data)
        this.noticeContentList.unshift({
          categoryId: 0,
          categoryName: '全部分类'
        })
      })
    },
    // 提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return
        }
        this.$http({
          url: this.$http.adornUrl('/shop/provision'),
          method: this.id ? 'put' : 'post',
          data: this.$http.adornData(this.dataForm)
        }).then(() => {
          this.$message({
            message: this.$i18n.t('outlet.success'),
            type: 'success'
          })
          this.visible = false
        })
      })
    },
    // 选择分类改变时同步改变dataForm中数据
    handleChange (value) {
      if (value.length > 0) {
        this.dataForm.categoryId = value[value.length - 1]
      } else {
        this.dataForm.categoryId = 0
      }
    },
    closeDialog () {
      this.$emit('refreshChange')
      this.$refs.dataForm.resetFields()
      this.id = 0
      this.selectedCategory = []
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
